﻿(function () {
    $(function () {
        
        var _$dataTable = $('#AccountsTable');
        
        var _dataService = abp.services.app.dataMaintain; //对应application中接口前缀
        //对应AppPermissions里面的Pages_BasicDataModule_Classrooms_Create等变量
        var _permissions = {
            create: abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.CreateAccount'),
            edit: abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.EditAccount'),
            'delete': abp.auth.hasPermission('Pages.FinanceModule.DataMaintains.DeleteAccount')
        };
        //新增/编辑账户窗体定义
        var _createOrEditBillAccountModal = new app.ModalManager({
            viewUrl: abp.appPath + 'FinanceModule/DataMaintains/CreateOrEditBillAccountModal',
            scriptUrl: abp.appPath + 'Areas/FinanceModule/Views/DataMaintains/_CreateOrEditBillAccountModal.js',
            modalClass: 'CreateOrEditBillAccountModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });

        //表格定义，包括样式，触发事件等。
        _$dataTable.jtable({

            //title: app.localize('Pages_BasicDataModule_Classrooms'),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getBillAccounts
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '20%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditBillAccountModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    deleteBillAccount(data.record);
                                });
                        }

                        return $span;
                    }
                },
                //网格里面字段必须驼峰写法
                accountName: {
                    title: app.localize('Pages_FinanceModule_BillAccounts_GridColumn_AccountName'),
                    width: '20%'
                },
                accountType: {
                    title: app.localize('Pages_FinanceModule_BillAccounts_GridColumn_AccountType'),
                    width: '10%'
                },
                schoolName: {
                    title: app.localize('Pages_FinanceModule_BillAccounts_GridColumn_SchoolName'),
                    width: '10%'
                },
                isTollAccount: {
                    title: app.localize('Pages_FinanceModule_BillAccounts_GridColumn_IsTollAccount'),
                    width: '10%'
                },
                balance: {
                    title: app.localize('Pages_FinanceModule_BillAccounts_GridColumn_Balance'),
                    width: '10%'
                },
                remark: {
                    title: app.localize('Pages_BasicDataModule_Classrooms_GridColumn_Remark'),
                    width: '20%'
                }
            }

        });//表格定义结束
        
        
        //加载数据方法
        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter: $('#DataTableFilter').val()
                });
            }
        }
        //删除账户信息
        function deleteBillAccount(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.accountName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _dataService.deleteBillAccount({
                            id: o.id
                        }).done(function () {
                            getObjects(true);
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }
        //账户查询框按钮
        $('#GetAccountsButton').click(function (e) {
            e.preventDefault();
            getObjects();
        });
        //打开新增/编辑页面
        $('#CreateNewButton').click(function (e) {
            e.preventDefault();
            _createOrEditBillAccountModal.open();
        });
        //新增/编辑保存之后回到主页面，重新加载主页面数据。
        abp.event.on('app.CreateOrEditModalSaved', function (result) {
            //alert('return value:' + result.classroomName);
            getObjects(true);
        });

        //加载数据
        getObjects();

        //光标定位到查询条件输入框
        $('#DataTableFilter').focus();
    });
})();